
#include "bsp_multi_adc.h"
#include "ntc_temp.h"


/* PTC 温度和电阻对应关系 */
const PTC_Temp_Res_T s_Temp_Res[] = 
{
    {-48, 467834},
    {-47, 438203},
    {-46, 410622},
    {-45, 384939},
    {-44, 361010},
    {-43, 338724},
    {-42, 317946},
    {-41, 298573},
    {-40, 280503},
    
    {-39, 263644},
    {-38, 247908},
    {-37, 233216},
    {-36, 206670},
    {-34, 194683},
    {-33, 183473},
    {-32, 172986},
    {-31, 163169},
    {-30, 153977},
    
    {-29, 145365},
    {-28, 137292},
    {-27, 129723},
    {-26, 122621},
    {-25, 115955},
    {-24, 109695},
    {-23, 103812},
    {-22, 98283},
    {-21, 93083},
    {-20, 88189},
    
    {-19, 83583},
    {-18, 79244},
    {-17, 75156},
    {-16, 71301},
    {-15, 67666},
    {-14, 64236},
    {-13, 60998},
    {-12, 57940},
    {-11, 55051},
    {-10, 52320},
    
    {-9, 49738},
    {-8, 47296},
    {-7, 44985},
    {-6, 42797},
    {-5, 40726},
    {-4, 38764},
    {-3, 36904},
    {-2, 35142},
    {-1, 33471},

    {0,  32032},
    {1,  30382},
    {2,  28956},
    {3,  27602},
    {4,  26316},
    {5,  25095},
    {6,  23935},
    {7,  22833},
    {8,  21786},
    {9,  20791},
    
    {10, 19844},
    {11, 18945},
    {12, 18089},
    {13, 17275},
    {14, 16501},
    {15, 15764},
    {16, 15063},
    {17, 14395},
    {18, 13760},
    {19, 13155},

    {20, 12578},
    {21, 12030},
    {22, 11507},
    {23, 11009},
    {24, 10534},
    {25, 9994},
    {26, 9651},
    {27, 9240},
    {28, 8848},
    {29, 8475},

    {30, 8119},
    {31, 7779},
    {32, 7455},
    {33, 7146},
    {34, 6850},
    {35, 6569},
    {36, 6300},
    {37, 6044},
    {38, 5799},
    {39, 5565},

    {40, 5342},
    {41, 5128},
    {42, 4925},
    {43, 4730},
    {44, 4544},
    {45, 4366},
    {46, 4196},
    {47, 4034},
    {48, 3878},
    {49, 3730},

    {50, 3588},
    {51, 3452},
    {52, 3321},
    {53, 3197},
    {54, 3078},
    {55, 2964},
    {56, 2854},
    {57, 2750},
    {58, 2649},
    {59, 2553},

    {60, 2461},
    {61, 2373},
    {62, 2289},
    {63, 2208},
    {64, 2130},
    {65, 2055},
    {66, 1984},
    {67, 1915},
    {68, 1849},
    {69, 1786},
    
    {70, 1725},
    {71, 1667},
    {72, 1611},
    {73, 1557},
    {74, 1505},
    {75, 1455},
    {76, 1408},
    {77, 1362},
    {78, 1317},
    {79, 1275},

    {80, 1234},
    {81, 1195},
    {82, 1157},
    {83, 1120},
    {84, 1085},
    {85, 1051},
    {86, 1018},
    {87, 987},
    {88, 956},
    {89, 927},

    {90, 899},
    {91, 872},
    {92, 845},
    {93, 820},
    {94, 796},
    {95, 772},
    {96, 749},
    {97, 727},
    {98, 706},
    {99, 685},

    {100, 665},
    {101, 646},
    {102, 627},
    {103, 609},
    {104, 592},
    {105, 575},
    {106, 559},
    {107, 543},
    {108, 528},
    {109, 513},
    
    {110, 498},
    {111, 484},
    {112, 471},
    {113, 458},
    {114, 445},
    {115, 433},
    {116, 421},
    {117, 409},
    {118, 398},
    {119, 387},
};

/***************************************************************************************************
 * @fn      ResToTemp
 *
 * @brief   使用查表法，把电阻值转换成温度
 *
 * @param	res 电阻值  
 *
 * @return	温度
 */
int8_t ResToTemp(uint32_t res)
{
    uint16_t i;
    uint16_t ArrSize;
    int16_t retTemp;

    /* 求表格数组元素个数 */
    ArrSize = sizeof(s_Temp_Res) / sizeof(PTC_Temp_Res_T);

    if (res >= 10)
    {
        retTemp = s_Temp_Res[0].temp;

        /* 遍历表格 */
        for (i = 0; i < ArrSize; i++)
        {
            if (res < s_Temp_Res[i].res)
            {
                retTemp = s_Temp_Res[i].temp;
                continue;
            }
            else
            {
                break;
            }
        }
    }
    else
    {
        retTemp = 10;
    }
 
    return retTemp;
}


/***************************************************************************************************
 * @fn      Get_PTC_Temperature
 *
 * @brief   获取当前的温度 
 *
 * @param	无  
 *
 * @return	温度 
 */
int16_t g_PTC_Temp;
int16_t Get_PTC_Temperature(void)
{
    int16_t temp;
    uint32_t res;
    uint16_t AdcVal;

    /* 用ADC采集计算得的电压计算出电阻值 */
    AdcVal = get_ntc_adc_value();
    res = (uint32_t)AdcVal * 10000 / (4095 - AdcVal);

    /* 用电阻值，查表获得温度 */
    temp = ResToTemp(res);
    g_PTC_Temp = temp;

    return temp;
}





